package com.yz.newspaper.controller;


import com.yz.newspaper.pojo.po.User;
import com.yz.newspaper.pojo.result.Result;
import com.yz.newspaper.pojo.unitPo.PageBean;
import com.yz.newspaper.service.IUserService;
import com.yz.newspaper.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户相关
 *
 * @author author
 * @since 2024-04-21
 */
@Api("用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 前台登录
     * @param userDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result<User> login(@RequestBody User userDto) {
        User user;
        if (userDto.getUsername() == null && userDto.getPassword() == null) {
            return Result.error("用户名或密码不能为空");
        }
        user = userService.login(userDto);

        if (user == null) {
            return Result.error("用户不存在");
        }
        if (!user.getPassword().equals(userDto.getPassword())) {
            return Result.error("密码错误");
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        String jwt = JwtUtils.generateJwt(claims);
        user.setToken(jwt);
        return Result.success("登录成功", user);
    }

    /**
     * 后台登录
     *
     * @param userDto
     * @return
     */
    @PostMapping("/h_login")
    @ApiOperation("后台登录")
    public Result<User> h_login(@RequestBody User userDto) {
        if (userDto.getPhone() == null && userDto.getPassword() == null) {
            return Result.error("用户名或密码不能为空");
        }
        User user = userService.login(userDto);

        if (user == null) {
            return Result.error("用户不存在");
        }
        if (!user.getPassword().equals(userDto.getPassword())) {
            return Result.error("密码错误");
        }
        if (user.getIsAdmin() != 1) {
            return Result.error("不是管理员");
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        String jwt = JwtUtils.generateJwt(claims);
        user.setToken(jwt);
        return Result.success("登录成功", user);
    }

    @GetMapping
    @ApiOperation("获取用户信息")
    public Result getUserAll(String username, @NotNull Integer pageNum, @NotNull Integer pageSize) {
        PageBean<User> users = userService.getUserAll(username, pageNum, pageSize);
        return Result.success(users);
    }

    @PostMapping
    @ApiOperation("添加用户")
    public Result addUser(@RequestBody User user) {
        return userService.save(user) ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation("修改用户")
    public Result updateUser(@RequestBody User user) {
        return userService.updateById(user) ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public Result deleteUser(@PathVariable Integer id) {
        return userService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }

}
